package com.lkm.app_ui.netapi;


import android.content.Context;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.MyApplication;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.AppHelp;
import com.lkm.app_ui.help.GsonHelp;
import com.lkm.app_ui.ui.fj.FjOrder;
import com.lkm.app_ui.ui.fj.to.FjOrderTo;
import com.lkm.frame.task.StopAble;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.ServerTimeSync;
import com.lkm.javatool.lang.NameValueP;
import com.lkm.market2_0.m.steelyarddata.SteelyardValue;
import com.lkm.market2_0.netapi.Api;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;



public class AppApi {



    public static final String getAPi(String act) {
//        http://zhongzi.web.ic1986.com/api/v1/balance/stall_qr改写后是https://zhongzi.web.ic1986.com/api/app/stall_qr
        return "https://zhongzi.web.ic1986.com/api/app/" + act;
    }

    

    public static String buttons(int classid, StopAble sa) {

        String ukey = RunTimeData.steelyardInfo == null ? "" : RunTimeData.steelyardInfo.getSteelyardMac();

        String re = getString( "http://zhongzi.web.ic1986.com/api/v1/balance/balance_breed_add", new NameValueP[]{
                new NameValueP("ukey", ukey),
                new NameValueP("classid", ""+classid)
        }, null, sa);
        return re;
    }

    

    public static String breed_class( StopAble sa) {

        String ukey = RunTimeData.steelyardInfo == null ? "" : RunTimeData.steelyardInfo.getSteelyardMac();

        String re = getString( "http://zhongzi.web.ic1986.com/api/v1/balance/balance_breed_class", new NameValueP[]{
                new NameValueP("ukey", ukey)
        }, null, sa);
        return re;
    }

    public static String stall_qr( StopAble sa) {

        String ukey = RunTimeData.steelyardInfo == null ? "" : RunTimeData.steelyardInfo.getSteelyardMac();
//        "http://zhongzi.web.ic1986.com/api/v1/balance/stall_qr"
        String re = getString( "http://zhongzi.web.ic1986.com/api/v1/balance/stall_qr", new NameValueP[]{
                new NameValueP("ukey", ukey)
        }, null, sa);
        return re;
    }

    

    public static String regBalanceTask(Map<String, Object> stringStringMap, StopAble sa) {

        String re = getString("http://zhongzi.web.ic1986.com/api/v1/balance/manufacturers_balance", new NameValueP[]{

        }, stringStringMap, sa);
        return re;
    }

    

    public static String versions(StopAble sa) {

        Map<String, Object> stringStringMap = new Hashtable<>();

        String ukey = RunTimeData.steelyardInfo == null ? "" : RunTimeData.steelyardInfo.getSteelyardMac();
        String oid = RunTimeData.steelyardInfo == null ? "" : (""+RunTimeData.steelyardInfo.getOrg());


        stringStringMap.put("curVersion", AppHelp.getLocalVersionName());

        String re = getString(getAPi("version"), new NameValueP[]{
                new NameValueP("appkey", "27977243")
                ,new NameValueP("ukey",ukey)
                ,new NameValueP("oid",oid)
        }, stringStringMap, sa);
        return re;
    }



    

    public static String get_balance_bluetooth(StopAble sa) {
        String ukey = RunTimeData.steelyardInfo == null ? "" : RunTimeData.steelyardInfo.getSteelyardMac();

        Map<String, Object> stringStringMap = new HashMap<>();
        stringStringMap.put("deviceUncode", AppConst.deviceUncode);

        String re = getString( "http://zhongzi.web.ic1986.com/api/v1/balance/get_balance_bluetooth",null, stringStringMap, sa);

        return re;
    }

    public static String loadPrintTemplate(StopAble sa) {
        String ukey = RunTimeData.steelyardInfo == null ? "" : RunTimeData.steelyardInfo.getSteelyardMac();
        String re = getString( "http://zhongzi.web.ic1986.com/api/v1/balance/balance_print", new NameValueP[]{
                new NameValueP("ukey", ukey)
        }, null, sa);

        return re;
    }
    

    public static String upAD(SteelyardValue steelyardValue, StopAble sa) {


        Map<String, Object> stringStringMap = GsonHelp.fromJson(GsonHelp.toJson(steelyardValue),new com.google.gson.reflect.TypeToken<Map<String, String>>() {}.getType());

        String re = getString("http://zhongzi.web.ic1986.com/api/v1/balance/adinfo", new NameValueP[]{

        }, stringStringMap, sa);
        return re;
    }

    

    public static String upSteelyardRegTask(Map<String, Object> stringStringMap, StopAble sa) {
        boolean hasreg = MyApplication.getInstance().getSharedPreferences("settingapp", Context.MODE_PRIVATE).getBoolean("device_hasReg",false);
        if(stringStringMap!=null){
            stringStringMap.put("status", hasreg ? 2 : 1);
        }

        String re = getString("http://zhongzi.web.ic1986.com/api/v1/balance/balance_info", new NameValueP[]{

        }, stringStringMap, sa);
        return re;
    }

    public static String loadAD(StopAble sa) {
        String ukey = RunTimeData.steelyardInfo == null ? "" : RunTimeData.steelyardInfo.getSteelyardMac();
        String re = getString( "http://zhongzi.web.ic1986.com/api/v1/balance/ad", new NameValueP[]{
                new NameValueP("ukey", ukey)
        }, null, sa);

        return re;
    }

    

    public static String serverSteelyardMac(String no,StopAble sa) {
        String ukey = RunTimeData.steelyardInfo == null ? "" : RunTimeData.steelyardInfo.getSteelyardMac();

        Map<String, Object> stringStringMap = new HashMap<>();
        stringStringMap.put("ukey", ukey);
        stringStringMap.put("key", ukey);
        stringStringMap.put("device_no", no);
        stringStringMap.put("device_type", "1");
        String re = getString("http://zhongzi.web.ic1986.com/api/v1/balance/get_balance_no", null, stringStringMap, sa);

        return re;
    }
    

    public static String fjGet(StopAble sa) {
        if (RunTimeData.steelyardInfo == null || RunTimeData.steelyardInfo.upDataApiTo == null)
            return "";

        String ukey = RunTimeData.steelyardInfo == null ? "" : RunTimeData.steelyardInfo.getSteelyardMac();

        Map<String, Object> stringStringMap = new HashMap<>();
        stringStringMap.put("ukey", ukey);
        stringStringMap.put("key", ukey);


//        RunTimeData.steelyardInfo.upDataApiTo.fjGetUrl="http://test.7chezhibo.com/api/2/scale/order_list";
        String re = getString(RunTimeData.steelyardInfo.upDataApiTo.fjGetUrl, null, stringStringMap, sa);

        return re;
    }

    

    public static String fjUp(FjOrder fjOrder, StopAble sa) {
        if (RunTimeData.steelyardInfo == null || RunTimeData.steelyardInfo.upDataApiTo == null)
            return "";

        String ukey = RunTimeData.steelyardInfo == null ? "" : RunTimeData.steelyardInfo.getSteelyardMac();

        Map<String, Object> stringStringMap = new HashMap<>();
        stringStringMap.put("ukey", ukey);
        stringStringMap.put("key", ukey);
        stringStringMap.put("order_no", fjOrder.fjInfoNo);
        stringStringMap.put("order_goods", FjOrderTo.by(fjOrder).order_goods);
//        RunTimeData.steelyardInfo.upDataApiTo.fjUpUrl = "http://test.7chezhibo.com/api/2/scale/order_notify";
        String re = getString(RunTimeData.steelyardInfo.upDataApiTo.fjUpUrl, null, stringStringMap, sa);

        return re;
    }

    public static String getString(String url, NameValueP[] params, Map<String, Object> stringStringMap, StopAble sa) {

        String content = stringStringMap == null ? "{}" : GsonHelp.toJson(stringStringMap);

        System.out.println("===== appapi "+url+(params==null?"":GsonHelp.toJson(params))+content);

        String oid = RunTimeData.steelyardInfo == null ? "" : "" + RunTimeData.steelyardInfo.getOrg();
        String ukey = RunTimeData.steelyardInfo == null ? "" : RunTimeData.steelyardInfo.getSteelyardMac();
        NameValueP[] heads = new NameValueP[]{
                new NameValueP("oid", oid),
                new NameValueP("ukey", ukey),
                new NameValueP("hostapi", Api.host)
        };

        String re = HttpRequest.getString(url, params, content, heads, sa);

//        System.out.println("===== appapi "+re);
        return re;
    }


    public static String mingle(List<NameValueP> params) {
        ServerTimeSync.getInstance().getServerCurTime();
        return "ccc";
    }




//            String md5 = MessageDigestHelp.getDigestHex(s.getValue().getBytes("utf-8"), MessageDigestHelp.MD5);
//            long l = Long.parseLong(md5.substring(0, 5), 16);


//        String md5 = MessageDigestHelp.getDigestHex(key.getBytes("utf-8"), MessageDigestHelp.MD5);
//        long l = Long.parseLong(md5.substring(0, 5), 16);

//        return MessageDigestHelp.getDigestHex(String.valueOf(result).getBytes(), MessageDigestHelp.MD5);

}
